#include "widget.h"




Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    PortComboBox = new QComboBox(this); //端口列表
    UpDataButton = new QPushButton("UpData",this); //升级按键
    AddBinButton = new QPushButton("AddBin",this); //添加bin文件按键
    FilePathLineEdit = new QLineEdit(this); //文件路径
    ProgressBar = new QProgressBar(this); //进度条
    BurnSelection = new QPushButton("DownOTA",this); //烧录选择
    layout = new QGridLayout(); //网格布局管理器

    otaThread = new OTAThread();

    this->setWindowFlags(Qt::WindowMinimizeButtonHint|Qt::WindowCloseButtonHint); //关闭最大化功能

    QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts(); //获取串口号
    foreach(const QSerialPortInfo &serialPortInfo, ports) //将串口号添加到 ComboBox 控件中
    {
        QString portName = serialPortInfo.portName();
        PortComboBox->addItem(portName);
    }

    ProgressBar->setRange(0, 100); //设置进度条范围
    ProgressBar->show();
    ProgressBar->setValue(90); //更新进度条的值

    FilePathLineEdit->setReadOnly(true); //设置文本框不可写

    layout->addWidget(FilePathLineEdit,0,0,1,2);
    layout->addWidget(AddBinButton,0,2,1,1);
    layout->addWidget(PortComboBox,1,0,1,1);
    layout->addWidget(BurnSelection,1,1,1,1);
    layout->addWidget(UpDataButton,1,2,1,1);
    layout->addWidget(ProgressBar,2,0,1,3);
    layout->setSpacing(10); //设置每个组件间的间隔
    layout->setColumnStretch(1, 9); //设置变化比例系数，以行/列来设置比例系数
    setLayout(layout);

    QObject::connect(AddBinButton,SIGNAL(clicked()),this,SLOT(openFile())); //信号槽连接
    QObject::connect(BurnSelection,SIGNAL(clicked()),this,SLOT(otaSwitch()));
    QObject::connect(UpDataButton,SIGNAL(clicked()),this,SLOT(upData()));
    QObject::connect(this, &Widget::messageToSend, otaThread, &OTAThread::handleMessage); //线程通讯用
}

Widget::~Widget()
{

}

bool Widget::openFile()
{
    filePath = QFileDialog::getOpenFileName(this,"打开文件",QDir::currentPath(),"All Files (*.*)");
    FilePathLineEdit->setText(filePath);
}

void Widget::otaSwitch()
{
    static uint8_t OTASwitch = 0;
    if(OTASwitch == 0)
    {
        OTASwitch = 1;
        BurnSelection->setText("UpOTA");
    }
    else
    {
        OTASwitch = 0;
        BurnSelection->setText("DownOTA");
    }
}

bool Widget::upData()
{
    qDebug() << "启动线程传递文件路径和端口号";
    SerialPort = PortComboBox->currentText();
    emit messageToSend(filePath,SerialPort); //对线程发送信号
    otaThread->start();
//    if(otaState == 0)
//    {
//        otaState = 1; //开始ota
//        UpDataButton->setText("clos");
//        serial->setPortName(PortComboBox->currentText()); //设置串口名称和波特率
//        serial->setBaudRate(QSerialPort::Baud9600);
//        if (!serial->open(QIODevice::ReadWrite)) // 打开串口
//        {
//            QMessageBox::critical(this, "Error", "can't open com");
//            return 1;
//        }
//        qDebug() << "打开串口: " << PortComboBox->currentText();
//        otaThread = new OTAThread();
//        otaThread->start();

//        // 发送数据
//        serial->write((const char *)fileBuff,fileLen);
//    }
//    else if(otaState == 1)
//    {
//        otaState = 0; //结束ota
//        UpDataButton->setText("UpData");
//        serial->close(); //关闭串口
//        otaThread->stop();
//    }
//    return 0;

//    sendYmodemFile(fileName,serial);
}
